/***************************************************************************************
 *
 *  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 *
 *  By downloading, copying, installing or using the software you agree to this license.
 *  If you do not agree to this license, do not download, install, 
 *  copy or use the software.
 *
 *  Copyright (C) 2014-2019, Happytimesoft Corporation, all rights reserved.
 *
 *  Redistribution and use in binary forms, with or without modification, are permitted.
 *
 *  Unless required by applicable law or agreed to in writing, software distributed 
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 *
****************************************************************************************/

#ifndef HT_PS_H
#define HT_PS_H

#include "../defineconfig.h"

#pragma pack(push) // 保存对齐状态
#pragma pack(1)
#define  GXX_PS_LEN 435

typedef struct ps_header
{
	uint8       system_header_start_code[4];		    //'0x000001BA'

	uint8       system_clock_reference_base21   : 2;
	uint8       marker_bit						: 1;
	uint8       system_clock_reference_base1    : 3;
	uint8       fix_bit							: 2;	//'01'

	uint8       system_clock_reference_base22;

	uint8       system_clock_reference_base31   : 2;
	uint8       marker_bit1						: 1;
	uint8       system_clock_reference_base23   : 5;

	uint8       system_clock_reference_base32;

	uint8       system_clock_reference_extension1	: 2;
	uint8       marker_bit2					    : 1;
	uint8       system_clock_reference_base33   : 5;	//system_clock_reference_base 33bit

	uint8       marker_bit3					    : 1;
	uint8       system_clock_reference_extension2: 7;	//system_clock_reference_extension 9bit

	uint8       program_mux_rate1;
	uint8       program_mux_rate2;

	uint8       marker_bit5					    : 1;
	uint8       marker_bit4					    : 1;
	uint8       program_mux_rate3			    : 6;

	uint8       pack_stuffing_length		    : 3;
	uint8       reserved					    : 5;
}PSHDR;

typedef struct ps_system_header
{
	uint8       system_header_start_code[4];		    //'0x000001BB'
	uint8       header_length[2];

	uint8       rate_bound1						: 7;
	uint8       marker_bit						: 1;

	uint8       rate_bound2;

	uint8       marker_bit1						: 1;
	uint8       rate_bound3						: 7;

	uint8       csps_flag					    : 1;
	uint8       fixed_flag						: 1;
	uint8       audio_bound						: 6;

	uint8       video_bound						: 5;
	uint8       marker_bit2						: 1;
	uint8       video_lock_flag					: 1;
	uint8       audio_lock_flag					: 1;

	uint8       reserved;

	uint8       stream_id1;
	uint8       std_buffer_size_bound11			: 5;
	uint8       std_buffer_bound_scale1			: 1;
	uint8       marker_bit13				    : 2;
	uint8       std_buffer_size_bound12;

	uint8       stream_id2;
	uint8       std_buffer_size_bound21			: 5;
	uint8       std_buffer_bound_scale2			: 1;
	uint8       marker_bit23				    : 2;
	uint8       std_buffer_size_bound22;
}PSSYSHDR;

typedef struct ps_system_map
{
	uint8       packet_start_code_prefix[3];
	uint8       map_stream_id;
	uint8       program_stream_map_length[2];

	uint8       program_stream_map_version		: 5;
	uint8       reserved1					    : 2;
	uint8       current_next_indicator			: 1;

	uint8       marker_bit						: 1;
	uint8       reserved2					    : 7;

	uint8       program_stream_info_length[2];
	uint8       elementary_stream_map_length[2];

	uint8       stream_type1;
	uint8       elementary_stream_id1;
	uint8       elementary_stream_info_length1[2];

	uint8       stream_type2;
	uint8       elementary_stream_id2;
	uint8       elementary_stream_info_length2[2];

	uint8       crc32[4];
}PSSYSMAP;

typedef struct pes_header
{
	uint8       packet_start_code_prefix[3];
	uint8       stream_id;
	uint8       PES_packet_length[2];

	uint8       original_or_copy :1;
	uint8       copyright :1;
	uint8       data_alignment_indicator :1;
	uint8       PES_priority :1;
	uint8       PES_scrambling_control :2;
	uint8       fix_bit :2;

	uint8       PES_extension_flag :1;
	uint8       PES_CRC_flag :1;
	uint8       additional_copy_info_flag :1;
	uint8       DSM_trick_mode_flag :1;
	uint8       ES_rate_flag :1;
	uint8       ESCR_flag :1;
	uint8       PTS_DTS_flags :2;

	uint8       PES_header_data_length;
}PESHDR;

typedef struct 
{
	uint8       marker_bit :1;
	uint8       PTS1 :3;
	uint8       fix_bit :4;

	uint8       PTS21;

	uint8       marker_bit1 :1;
	uint8       PTS22 :7;

	uint8       PTS31;

	uint8       marker_bit2 :1;
	uint8       PTS32 :7;

} PTS_TAG, *PPTS_TAG;

#pragma pack(pop) // 保存对齐状态


typedef struct ps_pkt_info
{
	uint32	    init_flag;

	uint32	    scr;
	uint32	    pts;

	PSHDR       ePSHeader;
	PSSYSHDR    ePSSHeader;
	PSSYSMAP    ePSMHeader;
	PESHDR      ePESHeader;
	PTS_TAG	    ePTS;

	uint8	    abuf[1024*8];
	int         abuf_offset;
} PSPKTI;


#ifdef __cplusplus
extern "C" {
#endif

/*
 * vtype : 0x1B - h264; 0x24 - h265; 0x10 - MPEG-4; 0x80 - SVAC
 * atype : 0x90 - G711; 0X92 - G722.1; 0X93 - G723.1; 0X99 - G729; 0X9B - SVACss
 */
void ps_init_info(PSPKTI * ps_pkt_i, int vtype, int atype);
int  ps_make_header(PSPKTI * ps_pkt_i, uint8 * header, uint32 framelen, uint32 framerate, int frametype, uint32 * pusedlen);


#ifdef __cplusplus
}
#endif


#endif


